//
//  UPMineInfomationViewController.m
//  Uang Pasti
//
//  Created by 李永彬 on 2025/8/17.
//

#import "UPMineInfomationViewController.h"
#import "UPGridView.h"
#import "UPGridItem.h"
#import "UPSheZhiViewController.h"
#import "UPMyLoanHistoryViewController.h"

@interface UPMineInfomationViewController ()

@property (nonatomic, strong) UIButton *headButton;
@property (nonatomic, strong) UILabel *welcomeLabel;
@property (nonatomic, strong) UILabel *idLabel;
@property (nonatomic, strong) UPGridView *gridView;

@end

@implementation UPMineInfomationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.bgImageView.image = [UIImage imageNamed:@"mine_bgImage"];
    self.hiddenTabBar = NO;
    [self commonInitSubviews];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.idLabel.text = [[UPUserDefaults stringForKey:loginPhoneNumberKey] maskMiddleCharacters];
}

- (void)commonInitSubviews {
    [self.view addSubview:self.headButton];
    [self.headButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.view).offset(66);
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.width.height.mas_equalTo(90);
    }];
    [self.view addSubview:self.welcomeLabel];
    [self.welcomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.headButton.mas_trailing).offset(10);
        make.trailing.equalTo(self.view).offset(-20);
        make.centerY.equalTo(self.headButton).offset(-15);
        make.height.mas_equalTo(20);
    }];
    
    [self.view addSubview:self.idLabel];
    [self.idLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.welcomeLabel);
        make.trailing.equalTo(self.view).offset(-20);
        make.centerY.equalTo(self.headButton).offset(15);
        make.height.mas_equalTo(20);
    }];
    
    [self.view addSubview:self.gridView];
    [self.gridView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.headButton.mas_bottom).offset(40);
        make.leading.trailing.equalTo(self.view).inset(20);
    }];
}

- (UIButton *)headButton {
    if (!_headButton) {
        _headButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_headButton setBackgroundImage:[UIImage imageNamed:@"myInfo_head"] forState:UIControlStateNormal];
        [_headButton addTarget:self action:@selector(headButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headButton;
}

- (UILabel *)welcomeLabel {
    if (!_welcomeLabel) {
        _welcomeLabel = [[UILabel alloc] init];
        _welcomeLabel.textColor = [UIColor colorWithHexString:@"#000000"];
        _welcomeLabel.textAlignment = NSTextAlignmentLeft;
        _welcomeLabel.font = [UIFont systemFontOfSize:14 weight:400];
        _welcomeLabel.text = LocalizableForKey(LocalizableForKey(@"welcome"));
    }
    return _welcomeLabel;
}

- (UILabel *)idLabel {
    if (!_idLabel) {
        _idLabel = [[UILabel alloc] init];
        _idLabel.textColor = [UIColor colorWithHexString:@"#000000"];
        _idLabel.textAlignment = NSTextAlignmentLeft;
        _idLabel.font = [UIFont systemFontOfSize:14 weight:400];
        _idLabel.text = [[UPUserDefaults stringForKey:loginPhoneNumberKey] maskMiddleCharacters];
    }
    return _idLabel;
}

- (UPGridView *)gridView {
    if (!_gridView) {
        UPGridItem *orderItem = [[UPGridItem alloc] initWithImage:[UIImage imageNamed:@"login_item_contanier1"] title:LocalizableForKey(@"Order")];
        UPGridItem *onlineItem = [[UPGridItem alloc] initWithImage:[UIImage imageNamed:@"login_item_contanier2"] title:LocalizableForKey(@"Online Service")];
        UPGridItem *privacyItem = [[UPGridItem alloc] initWithImage:[UIImage imageNamed:@"login_item_contanier3"] title:LocalizableForKey(@"Privacy Agreement")];
        UPGridItem *setUpItem = [[UPGridItem alloc] initWithImage:[UIImage imageNamed:@"login_item_contanier4"] title:LocalizableForKey(@"Set up")];

        [orderItem addTarget:self action:@selector(orderItemClicked:) forControlEvents:UIControlEventTouchUpInside];
        [orderItem.button addTarget:self action:@selector(orderItemClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [onlineItem addTarget:self action:@selector(onlineItemClicked:) forControlEvents:UIControlEventTouchUpInside];
        [onlineItem.button addTarget:self action:@selector(onlineItemClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [privacyItem addTarget:self action:@selector(privacyItemClicked:) forControlEvents:UIControlEventTouchUpInside];
        [privacyItem.button addTarget:self action:@selector(privacyItemClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        [setUpItem addTarget:self action:@selector(setUpItemClicked:) forControlEvents:UIControlEventTouchUpInside];
        [setUpItem.button addTarget:self action:@selector(setUpItemClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        _gridView = [[UPGridView alloc] initWithItems:@[orderItem, onlineItem, privacyItem, setUpItem]];
    }
    return  _gridView;
}

#pragma action
- (void)headButtonAction: (UIButton *)button {

}

- (void)orderItemClicked: (UIButton *)button {
    UPMyLoanHistoryViewController *orderVC = [[UPMyLoanHistoryViewController alloc] init];
    [self.navigationController pushViewController:orderVC animated:YES];
}

- (void)onlineItemClicked: (UIButton *)button {
    NSLog(@"item2");
    NSString *url = [NSString stringWithFormat:@"%@%@", kH5Domain, @"growthaneous"];
    [self pushToWebViewController:url];
}

- (void)privacyItemClicked: (UIButton *)button {
    NSLog(@"item3");
    NSString *url = [NSString stringWithFormat:@"%@%@", kH5Domain, @"reportform"];
    [self pushToWebViewController:url];
}

- (void)setUpItemClicked: (UIButton *)button {
    UPSheZhiViewController *setVC = [[UPSheZhiViewController alloc] init];
    [self.navigationController pushViewController:setVC animated:YES];
}

-(void)pushToWebViewController: (NSString *)url {
    
    UPWebViewController *webVC = [[UPWebViewController alloc] init];
    webVC.urlString = url;
    webVC.isFollowOrder = false;
    [[UIApplication up_currentViewController].navigationController pushViewController:webVC animated:YES];
}

@end
